package com.easydisk.file.controller;

import com.easydisk.file.annotation.NeedShareCode;
import com.easydisk.file.model.req.*;
import com.easydisk.file.model.vo.UserFileVO;
import com.easydisk.file.model.vo.UserShareDetailVO;
import com.easydisk.file.model.vo.UserShareSimpleDetailVO;
import com.easydisk.file.model.vo.UserShareUrlVO;
import com.easydisk.file.service.ShareService;
import com.easydisk.response.ApiResponse;
import com.easydisk.common.util.SecurityContextUtil;
import com.easydisk.common.util.ShareIdUtil;
import com.easydisk.common.util.UserIdUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author word
 */
@RestController
@RequestMapping("/file")
public class ShareController {

    @Autowired
    private ShareService shareService;

    /**
     * 创建分享链接
     *
     */
    @ApiOperation(
            value = "创建分享链接",
            notes = "该接口提供了创建分享链接的功能")
    @PostMapping("share")
    public ApiResponse<UserShareUrlVO> create(@Validated @RequestBody AddShareReq addShareReq) {
        Long userId = SecurityContextUtil.getUserId();
        UserShareUrlVO userShareUrlVO = shareService.addShare(addShareReq.getShareName(), addShareReq.getShareType(),
                addShareReq.getShareDayType(), addShareReq.getShareFileIds(), userId);
        return ApiResponse.data(userShareUrlVO);
    }

    /**
     * 查询分享列表
     *
     */
    @ApiOperation(
            value = "查询分享列表",
            notes = "该接口提供了查询分享列表的功能",
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE
    )
    @GetMapping("shares")
    public ApiResponse<List<UserShareUrlVO>> list() {
        Long userId = SecurityContextUtil.getUserId();
        return ApiResponse.data(shareService.shareList(userId));
    }


    /**
     * 取消分享（支持批量）
     *
     */
    @ApiOperation(
            value = "取消分享（支持批量）",
            notes = "该接口提供了取消分享（支持批量）的功能"
    )
    @DeleteMapping("share")
    public ApiResponse<String> cancelShares(@Validated @RequestBody CancelShareReq cancelShareReq) {
        Long userId = SecurityContextUtil.getUserId();
        shareService.cancelShares(cancelShareReq.getShareIds(), userId);
        return ApiResponse.success();
    }


    /**
     * 获取分享详情
     *
     */
    @ApiOperation(
            value = "获取分享详情",
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE
    )
    @GetMapping("share")
    @NeedShareCode
    public ApiResponse<UserShareDetailVO> getShareDetail() {
        // 获取分享人的用户id
        Long shareId = ShareIdUtil.get();
        return ApiResponse.data(shareService.getShareDetail(shareId));
    }


    /**
     * 获取分享详情-简单
     *
     */
    @ApiOperation(
            value = "获取简单分享详情"
    )
    @GetMapping("share/simple")
    public ApiResponse<UserShareSimpleDetailVO> getShareSimpleDetail(@NotNull(message = "分享ID不能为空")
                                                       @RequestParam(value = "shareId", required = false) Long shareId) {
        return ApiResponse.data(shareService.getShareSimpleDetail(shareId));
    }

    /**
     * 校验分享码
     *
     */
    @ApiOperation(
            value = "校验分享码",
            notes = "该接口提供了校验分享码的功能"
    )
    @PostMapping("/share/code/check")
    public ApiResponse<String> checkShareCode(@Validated @RequestBody CheckShareCodeReq checkShareCodeReq) {
        return ApiResponse.data(shareService.checkShareCode(checkShareCodeReq.getShareId(), checkShareCodeReq.getShareCode()));
    }

    /**
     * 获取下一级的文件列表
     *
     */
    @ApiOperation(
            value = "获取下一级的文件列表",
            notes = "该接口提供了获取下一级的文件列表的功能",
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE
    )
    @GetMapping("share/file/list")
    @NeedShareCode
    public ApiResponse<List<UserFileVO>> getShareUserFileList(@NotNull(message = "父id不能为空") @RequestParam(value = "parentId", required = false) Long parentId) {
        Long shareId = ShareIdUtil.get();
        return ApiResponse.data(shareService.getShareUserFileList(shareId, parentId));
    }

    /**
     * 保存至我的网盘
     *
     */
    @ApiOperation(
            value = "保存至我的网盘",
            notes = "该接口提供了保存至我的网盘的功能"
    )
    @PostMapping("share/save")
    @NeedShareCode
    public ApiResponse<String> addShareToNetDisk(@Validated @RequestBody SaveShareReq saveShareReq) {
        Long userId = SecurityContextUtil.getUserId();
        Long shareId = ShareIdUtil.get();
        shareService.addShareToNetDisk(shareId, saveShareReq.getFileIds(),
                saveShareReq.getTargetParentId(), userId);
        return ApiResponse.success();
    }

    /**
     * 分享文件下载
     *
     */
    @ApiOperation(value = "分享文件下载")
    @GetMapping("share/file/download")
    @NeedShareCode
    public void downloadShare(@NotNull(message = "文件id不能为空")
                             @RequestParam(value = "fileId", required = false) Long fileId,
                         HttpServletResponse response) {
        Long shareId = ShareIdUtil.get();
        shareService.downloadShare(shareId, fileId, response);
    }



}
